//index.js
//获取应用实例
var app = getApp()
Page({
    data: {
        motto: 'Hello World',
        userInfo: {}
    },
    //事件处理函数
    bindViewTap: function () {
        wx.navigateTo({
            url: '../logs/logs'
        })
    },
    onLoad: function () {
        console.log('onLoad')
        var that = this
        //调用应用实例的方法获取全局数据
        app.getUserInfo(function (userInfo) {
            //更新数据
            that.setData({
                userInfo: userInfo
            })
        })
    },
    authorize: function () {
        console.info("authorize:function");
        wx.authorize({
            scope: 'scope.userInfo',
            success() {
                // 用户已经同意小程序使用录音功能，后续调用 wx.startRecord 接口不会弹窗询问
                console.info("CCCCC");
            }
        });
    },
    login: function () {
        console.info("login:function");


        // 检查用户session是否过期
        wx.checkSession({
            success: function () {
                //session 未过期，并且在本生命周期一直有效
                console.info("已登陆！");
                wx.getUserInfo({
                    withCredentials: true,
                    success: function (res) {
                        console.info("用户信息:" + JSON.stringify(res));
                    }
                })
            },
            fail: function () {
                //登录态过期,重新登录 
                wx.login({
                    success: function (res) {
                        if (res.code) {
                            console.info("获取loginCode:" + res.code);
                            //发起网络请求,获取session信息(session_key和openId)
                            wx.request({
                                url: 'https://walleiptxcx.tunnel.qydev.com/api/getXcxSessionInfo/' + res.code + '.json',
                                success: function (data, statusCode, header) {
                                    var code = data.data.code;
                                    var openid = data.data.data.openid;
                                    var session_key = data.data.data.session_key;
                                    if (code == 0 && openid != undefined && session_key != undefined) {
                                        console.info("openId:" + openid + ";session_key:" + session_key);
                                        wx.getUserInfo({
                                            withCredentials: true,
                                            success: function (res) {
                                                console.info("用户信息:" + JSON.stringify(res));
                                            }
                                        })
                                    }
                                    else {
                                        console.info("未获取到session信息,data:" + JSON.stringify(data));
                                    }
                                }
                            })

                        } else {
                            console.log('获取用户登录态失败！' + res.errMsg)
                        }
                    }
                });
            }
        });


    }
})
